/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

#include "TypeMeta.h"

#include "MetaBasic.h"

#include "../common/BigEndianUnsignedByte.h"

type_name meta_basic_name = { "meta.basic" };

static int meta_basic_bind( type_element * element, type_definition * definition )
{
	int rc;
	
	rc = type_object_bind( &element->object, definition->library, &meta_basic_name );
	if ( rc != TYPE_OK ) return rc;

	rc = type_element_bind( element, definition );
	if ( rc != TYPE_OK ) return rc;
	
	return TYPE_OK;
}

void meta_basic_init( meta_basic * mb, int width, int flags )
{
	type_element_init( &(mb->expression.element), &meta_basic_bind );
	mb->width = width;
	mb->flags = flags;
}

void * meta_basic_construct( type_list * list, type_map * map )
{
	type_item * item = type_list_head( list );
	int width = (int) type_item_get_u8( item );
	item = type_item_next( item );
	int flags = (int) type_item_get_u8( item );
	
	meta_basic * mb = (meta_basic *) type_malloc( sizeof( meta_basic ) );
	meta_basic_init( mb, width, flags );
	return mb;
}

int meta_basic_read( type_input_stream * in, type_element * element )
{
	return type_reader_auto( in, element, meta_basic_construct );
}

int meta_basic_write( type_output_stream * out, void * o, type_element * element )
{
	meta_basic * mb = (meta_basic *) o;
	
	/* FIXME should send correct types. */
	type_output_write_named_object( out, &uint8_name, &mb->width );
	type_output_write_named_object( out, &uint8_name, &mb->flags );
	return TYPE_OK;	
}

int meta_basic_do_read( type_input_stream * in, type_element * element )
{
	/* basic types are atomic and must have specific readers */
	type_input_set_error( in, TYPE_READ_ERROR );
	return TYPE_EOF;
}

int meta_basic_do_write( type_output_stream * out, void * o, type_element * element )
{
	/* basic types are atomic and must have specific writers */
	type_output_set_error( out, TYPE_WRITE_ERROR );
	return TYPE_EOF;
}

